From Questions to Queries: Building an AI Agent That Speaks Database

How we created a universal database assistant that converts natural language into database queries, no SQL knowledge required.
The Challenge: Making Databases Accessible to Everyone
Imagine a sales manager asking, "What were our top 5 customers by revenue last quarter?" and getting an instant answer, without writing a single line of SQL. Or a marketing team scheduling automated weekly reports that query the database and email summaries every Monday morning, all through simple conversations.
This is the reality we built with the Database Assistant Agent, a multi-agent AI system that bridges the gap between natural language and database operations. The core innovation? Converting everyday questions into precise database queries automatically.
Natural Language to Query Conversion
The heart of our system is its ability to understand what users mean and translate that into the right database query. Here's how it works:
Understanding Intent
When a user asks "Show me customers who haven't ordered in 30 days," the agent doesn't just match keywords. It understands:
-
What they want: customer information
-
Which customers: those without recent orders
-
How recent: within the last 30 days
-
What format: a list of results
Schema Discovery
Before generating a query, the agent automatically discovers the database structure. It understands:
-
Available tables and their relationships
-
Field names and data types
-
How tables connect to each other
This happens in real-time through GraphQL introspection—no manual schema documentation needed.
Query Generation
The agent then constructs the appropriate GraphQL query (which Hasura translates to the underlying database query). It handles:
-
Simple lookups: "Find customer with ID 123"
-
Aggregations: "What's the total sales this month?"
-
Filters: "Show orders over $1,000"
-
Joins: "List customers with their order history"
-
Sorting: "Top 10 products by sales"
All without the user knowing anything about SQL, GraphQL, or database structure.
The Architecture: Four Specialized Agents Working Together
Our system uses a multi-agent architecture where specialized agents handle different aspects of the workflow.
The Super Agent: The Smart Router
The Super Agent is the entry point. It analyzes user requests and routes them to the right
specialist:
specialist:
-
Database questions → GraphQL Agent
-
Email requests → Email Agent
-
Automation setup → Scheduler Agent
Think of it as a knowledgeable receptionist who knows exactly which department can help.
The GraphQL Agent: The Query Expert
This is where the natural language to query conversion happens. The GraphQL Agent:
-
Uses Hasura MCP (Model Context Protocol) tools to discover schemas
-
Translates natural language into GraphQL queries
-
Executes queries and formats results
-
Works with any database Hasura supports (PostgreSQL, MySQL, SQL Server, MongoDB,
-
etc.)
The key advantage: Hasura acts as a universal adapter, so the agent only needs to understand GraphQL, not each database type.
The Email Agent: The Communicator
Handles all email operations through Microsoft O365 MCP:
-
Composes professional emails from natural language
-
Formats query results into readable reports
-
Sends emails automatically as part of workflows
The Scheduler Agent: The Automation Engine
Executes scheduled tasks autonomously:
-
Runs recurring queries (daily reports, hourly monitoring)
-
Delegates to GraphQL Agent for database queries
-
Delegates to Email Agent for sending results
-
Maintains per-user isolation
Real-World Example: From Question to Result
Here's what happens when a user asks: "Create a daily report that runs every morning at 9 AM. Query yesterday's sales and email the summary to manager@company.com"
-
User asks in natural language
-
Super Agent identifies this as an automation request
-
Automation created and scheduled for 9 AM daily
-
Next morning at 9 AM, Scheduler Agent wakes up
-
Scheduler Agent asks GraphQL Agent: "Query yesterday's sales"
-
GraphQL Agent converts to query, executes, and returns results
-
Scheduler Agent asks Email Agent: "Send these results to manager@company.com"
-
Email Agent formats and sends the email
The user never sees SQL, GraphQL, or any technical details, just the results they need.
Challenges We Overcame
Challenge 1: Accurate Schema Understanding
The Problem: Initially, we tried converting GraphQL introspection results to markdown and feeding the entire schema to the LLM. This approach had two major issues: the agent often
used incorrect table names or field types, and feeding the whole introspection result consumed a massive amount of tokens, making it expensive and slow.
used incorrect table names or field types, and feeding the whole introspection result consumed a massive amount of tokens, making it expensive and slow.
The Solution: We switched to dynamic schema discovery through Hasura MCP. Instead of static markdown knowledge, the agent now uses MCP tools that provide real-time, accurate schema information on-demand. This ensures accuracy while dramatically reducing token usage and costs.
Challenge 2: Query Accuracy
The Problem: Complex queries with multiple conditions or joins sometimes needed refinement.
The Solution: The agent now uses Hasura MCP tools that provide schema context, helping it construct more accurate queries. When uncertain, it asks clarifying questions.
Current Limitations
While the system handles most common use cases, there are some limitations:
-
Hasura Requirement: The system requires Hasura GraphQL Engine. If you don't use Hasura, you'll need to set it up first, though this is a one-time setup that enables universal database access.
-
Complex Query Limitations: Some complex queries with multiple table joins and aggregations can be challenging. The agent sometimes fails to generate the correct query on the first attempt, requiring user clarification or query refinement. While it handles most common scenarios well, highly complex multi-table operations may need iterative refinement.
-
Query Performance: The agent doesn't optimize queries for performance, it focuses on accuracy. For production systems with large datasets, query optimization may be needed.
Looking Ahead
The future of database interaction is natural language. As AI models improve and MCP protocols mature, we'll see even more sophisticated query generation, better handling of complex scenarios, and support for write operations with appropriate safety measures. The foundation we've built, multi-agent orchestration, MCP integration, and database abstraction, provides a scalable architecture that can grow with these capabilities.